// pages/shopManage/shopManage.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartLists:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          userInfo: wx.getStorageSync("userInfo"),
          userTypeName: wx.getStorageSync("userTypeName"),
          imageUrl: app.globalData.imageUrl
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getRemotestock(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  addQuantity: function (e) {//增加单个商品数量
    changeCartItemQuantity(this,'+', e)
  },

  minusQuantity: function (e) {//减少单个商品数量
    changeCartItemQuantity(this,'-', e)
  },
})

function changeCartItemQuantity (that, op, e) {//改变单个商品数量
  var cartLists = that.data.cartLists//找到储存在本地购物车的商品信息
  var cart = cartLists.filter(function (ele) {//过滤出点击加减的商品在本地储存的唯一商品ID
    return ele.id === e.currentTarget.dataset.id
  })[0]
  if (op === '-' && cart.inventory > 1) {//如果符号为－，则商品数量减一
    wx.request({
      url: app.globalData.allUrl + "/superMarketAction/updateEntrepotNum",
      data: {
        // userID:  wx.getStorageSync('userAgentId'),
        num: cart.inventory-1,
        productId: cart.id
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        if (res.data == 0) {
          cart.inventory=0
        } else if (res.data) {
          cart.inventory=res.data
        }
        that.setData({ cartLists: cartLists })//同时将所修改的数量保存到当前页面的购物车中
      }
    })
  } else if (op === '+') {//如果符号为+，则商品数量加一
    //console.log("如果符号为+"+cart.inventory);
    wx.request({
      url: app.globalData.allUrl + "/superMarketAction/updateEntrepotNum",
      data: {
        // userID:  wx.getStorageSync('userAgentId'),
        num: cart.inventory + 1,
        productId: cart.id
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        if (res.data == 0) {
          cart.inventory = 0
        } else if (res.data) {
          cart.inventory = res.data
        }
        that.setData({ cartLists: cartLists })//同时将所修改的数量保存到当前页面的购物车中
      }
    })
  } else if (op === '-' && cart.inventory == 1) {
    wx.request({
      url: app.globalData.allUrl + "/superMarketAction/updateEntrepotNum",
      data: {
        // userID:  wx.getStorageSync('userAgentId'),
        num: 0,
        productId: cart.id
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        if (res.data == 0) {
          cart.inventory = 0
        } else if (res.data) {
          cart.inventory = res.data
        }
        that.setData({ cartLists: cartLists })//同时将所修改的数量保存到当前页面的购物车中
      }
    })
  }
//  that.setData({ cartLists: cartLists })//同时将所修改的数量保存到当前页面的购物车中
}

//获取远端库存
function getRemotestock(that){
  wx.request({
    url: app.globalData.allUrl + "/superMarketAction/getSuperMarketEntrepotByAgentId",
    data: {
      agentId: wx.getStorageSync('userAgentId')
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      console.log(res.data);
      that.setData({
        cartLists:res.data,
      })
    }
  })
}

//改变远端库存
function changeRemotestock(that,count,id) {
  console.log("当前库存数：" + count)
  wx.request({
    url: app.globalData.allUrl + "/superMarketAction/updateEntrepotNum",
    data: {
     // userID:  wx.getStorageSync('userAgentId'),
      num: count,
      productId: id
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data==0){
      }else if(res.data){
      }
      that.setData({ cartLists: cartLists })
    }
  })
}